package com.itheima.个人代码.IO流2;

import java.io.*;
import java.util.Arrays;
import java.util.Comparator;

public class Demo11 {
    public static void main(String[] args) throws IOException {
        //sortfileline();
        //sortfileline1();
        //sortfiledemo3();

    }

    public static void sortfiledemo3() throws IOException {
        BufferedReader br = new BufferedReader(new FileReader("IO流2和对象操作流properties\\stu1.txt"));
        BufferedWriter bw = new BufferedWriter(new FileWriter("IO流2和对象操作流properties\\stu2.txt", true));
        String line;
        while ((line = br.readLine()) != null) {
            String[] strings = line.split(",");
            Arrays.sort(strings,(o1,o2)->Integer.parseInt(o1)-Integer.parseInt(o2));
            Arrays.stream(strings).forEach(s->{
                try {
                    bw.write(s+" ");
                } catch (IOException e) {
                    e.printStackTrace();
                }
            });
//            for (String string : strings) {
//                bw.write(string+" ");
//            }
            bw.newLine();
        }
        br.close();
        bw.close();
    }

    public static void sortfileline1() throws IOException {
        BufferedReader br = new BufferedReader(new FileReader("IO流2和对象操作流properties\\stu1.txt"));
        String line = br.readLine();
        String[] strings = line.split(",");
        int[] arr = new int[strings.length];
        for (int i = 0; i < strings.length; i++) {
            int math = Integer.parseInt(strings[i]);
            arr[i] = math;
        }
        Arrays.sort(arr);
        BufferedWriter bw = new BufferedWriter(new FileWriter("IO流2和对象操作流properties\\stu1.txt", true));
        bw.newLine();
        bw.write(Arrays.toString(arr));
        bw.close();

        br.close();
    }

    public static void sortfileline() throws IOException {
        BufferedReader br = new BufferedReader(new FileReader("IO流2和对象操作流properties\\stu1.txt"));
        String line;
        while ((line = br.readLine()) != null) {
            String[] strings = line.split(",");
            int[] arr = new int[strings.length];
            for (int i = 0; i < strings.length; i++) {
                int math = Integer.parseInt(strings[i]);
                arr[i] = math;
            }
            Arrays.sort(arr);
            BufferedWriter bw = new BufferedWriter(new FileWriter("IO流2和对象操作流properties\\stu1.txt", true));
            bw.newLine();
            bw.write(Arrays.toString(arr));
            bw.close();
        }
        br.close();
    }

}
